package com.dw.etl.core.springbatch.job;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.util.SpringBeanUtil;
import com.dw.etl.core.api.EtlBaseFieldService;
import com.dw.etl.core.api.EtlBaseService;
import com.dw.etl.core.api.EtlRunService;
import com.dw.etl.core.bean.model.BaseDbTpcomp;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.bean.model.EtlBaseField;
import com.dw.etl.core.utils.EtlUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lijl on 2021/6/29.
 */
@Component
public class HosToOdsJobHandler {
    @Resource(name = "myJobLauncher")
    SimpleJobLauncher myJobLauncher;

    @Autowired
    EtlBaseService etlBaseService;

    @Autowired
    EtlBaseFieldService etlBaseFieldService;


    @Autowired
    EtlRunService etlRunService;

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("hosToOdsJob")
    public void hosToOdsJob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date dtBegin = simpleDateFormat.parse("2020-01-01 00:00:00");
        Date dtEnd = simpleDateFormat.parse("2020-01-01 23:59:59");
        int counterStop = 0;//计算器，防止死循环，无实际意义
        Job job;
        EtlBase etlBaseModel = new EtlBase();
        etlBaseModel.setIdBase("4ae63bef061840098250ebf9eee83e94");
        List<EtlBase> etlBaseList = etlBaseService.findAllByModel(etlBaseModel);
        for (EtlBase etlBase : etlBaseList) {
            if (etlBase.getFgCreAuto() == 1) {
                etlRunService.autoCreatTable(etlBase.getIdBase());//自动建表
            }
        }
        try {
            JobParameters parameters;
            Date dtNext;
            JobExecution jobExecution;
            while (dtBegin.before(dtEnd) && counterStop < 10000) {
                dtNext = EtlUtil.getNextDate(dtBegin, 3);
                for (EtlBase etlBase : etlBaseList) {
                    parameters = new JobParametersBuilder()
                            .addDate("dtBegin", dtBegin)
                            .addDate("dtEnd", dtNext)
                            .addDate("dtNow", new Date())
                            .addString("tableName", etlBase.getTbTar())
                            .addString("idBase", etlBase.getIdBase())
                            .addLong("idDbSou", etlBase.getIdDbSou())
                            .addLong("idDbTar", etlBase.getIdDbTar())
                            .addString("qSql", etlBase.getQsql()).toJobParameters();
                    job = (Job) SpringBeanUtil.getBean(etlBase.getIdJb());
                    jobExecution = myJobLauncher.run(job, parameters);
                }
                dtBegin = dtNext;
                counterStop++;
            }
        } catch (Exception e) {
            throw new BaseException("抽取任务异常", e);
        }
    }

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("hosToOdsJob1")
    public void hosToOdsJob1() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println(param);
    }
}
